﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Collections;

namespace PokemonRuby
{
    public partial class Form1 : Form
    {
        public int cellW;
        public int cellH;
        public int viewW; //  viewport la 1 hinh vuong 9x9 trong long hinh vuong 15x15
        public int viewH;

        //public Map objectMap = null;
        Thread threadQuanLyNhanVatPhu = null;
        Thread threadXuLyNhanVatPhuVaCayCanh = null;
        protected enum MovingDirection
        {
            Left,
            Right,
            Up,
            Down
        }


        public Map WorldMap = null;
        public Map MiniMap = null;
        //protected string appPath = null;
        protected Hashtable htMapPattern = new Hashtable();
        protected Hashtable htCharacterPattern = new Hashtable();
        protected int mapW = 0; // 15
        protected int mapH = 0; // 15
        protected int mapD = 0; // 15
        protected int viewX = 0;
        protected int viewY = 0;
        protected GameCharacter character1 = new GameCharacter();
        protected string mapFolder = null;
        ArrayList listCharacter = null;
        public Hashtable htMinorCharacterArray = new Hashtable();
        
        public Form1()
        {
            InitializeComponent();

            cellW = 48;
            cellH = 48;
            viewW = 29; //  viewport la 1 hinh vuong 9x9 trong long hinh vuong 15x15
            viewH = 20;

            FileInfo fi = new FileInfo(Application.ExecutablePath);
            GlobalVariable.appPath = fi.Directory.FullName;

            this.Width = (this.Width - this.ClientSize.Width) + (this.viewW * this.cellW);
            this.Height = (this.Height - this.ClientSize.Height) + (this.viewH * this.cellH);

            GlobalVariable.kichBan = 1;
            GlobalVariable.choice_1 = 0;
            GlobalVariable.choice = 0;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            this.LoadGameResource();
            MinorCharacter.LoadMinorCharacter();
            string path = Path.Combine(GlobalVariable.appPath, "WorldMaps");
            WorldMap = new Map(path);

            SetMinorByMap();

            WorldMap.LoadMetaMap(0);
            WorldMap.LoadMap(0);

            this.mapW = WorldMap.MapW;
            this.mapH = WorldMap.MapH;
            this.mapD = WorldMap.MapD;

            character1.LoadResourceCharacter();
            character1 = character1.LoadGameCharacter();

            threadQuanLyNhanVatPhu = new Thread(new ThreadStart(ManagerMinorChacracter));
            threadQuanLyNhanVatPhu.Start();         

        }
        public void SetMinorByMap()
        {
            ArrayList arr = new ArrayList();

            MinorCharacter minor1 = new MinorCharacter();
            // kichBan = 1
            string msg1 = "Chào lị à, ngộ tạ lị xị à. Ta sẽ cho ngộ con Pokemon. Sau đó tìm một nhân vật tên là Phạm Hữu Sâm để thách đấu với hắn.";
            // kichBan = 2
            string msg2 = "Bạn đã có Pokemon, hãy thách đấu với Phạm Hữu Sâm.";
            // kichBan = 4
            string msg3 = "Hãy đến gặp tên Phạm Hữu Sâm, hắn sẽ giúp ích cho cậu.";
            minor1.htMessage.Add(1, msg1);
            minor1.htMessage.Add(2, msg2);
            minor1.htMessage.Add(3, "");
            minor1.htMessage.Add(4, msg3);
            minor1.name = "Tri";
            minor1 = minor1.CreateMinorCharacter(minor1, 20, 31, 9, 12, 0);
            arr.Add(minor1);

            MinorCharacter minor2 = new MinorCharacter();
            // kichBan = 1
            string msg4 = "Ngộ là Phạm Hữu Sâm xin chào lị à, hãy tìm 1 pokemon mạnh nhất đến đây gặp ta!";
            // kichBan = 2
            string msg5 = "Có muốn đấu với ngộ 1 trận ra trò không nào con gà kia?";
            // kichBan = 4
            string msg6 = "Tôi đã thua cậu. Hãy gặp Tân Rùi và đấu với anh ấy, anh ấy là sư phụ tôi.";
            minor2.htMessage.Add(1, msg4);
            minor2.htMessage.Add(2, msg5);
            minor2.htMessage.Add(3, "");
            minor2.htMessage.Add(4, msg6);
            minor2.name = "Sam";
            minor2 = minor2.CreateMinorCharacter(minor2, 1, 12, 15, 12, 0);
            arr.Add(minor2);

            htMinorCharacterArray.Add((int)0, arr);
            //////////////////////////////////////////////////////////////////////////////////////////////////
            arr = new ArrayList();

            MinorCharacter minor3 = new MinorCharacter();
            minor3.name = "TanRui";
            minor3 = minor3.CreateMinorCharacter(minor3, 20, 31, 5, 8, 0);
            arr.Add(minor3);

            MinorCharacter minor4 = new MinorCharacter();
            minor4 = minor4.CreateMinorCharacter(minor4, 1, 12, 6, 6, 0);
            arr.Add(minor4);

            htMinorCharacterArray.Add((int)1, arr);
            //////////////////////////////////////////////////////////////////////////////////////////////////
            arr = new ArrayList();
            MinorCharacter minor5 = new MinorCharacter();
            minor5 = minor5.CreateMinorCharacter(minor5, 1, 12, 6, 6, 0);
            arr.Add(minor5);

            htMinorCharacterArray.Add((int)2, arr);
        }
        public void ListMinorCharacter()
        {
            listCharacter = new ArrayList();
            listCharacter = (ArrayList)htMinorCharacterArray[0];
        }
        public void ManagerMinorChacracter()
        {
            try
            {
                ListMinorCharacter();
                threadXuLyNhanVatPhuVaCayCanh = new Thread(new ThreadStart(MovingMinorCharacter));
                threadXuLyNhanVatPhuVaCayCanh.Start();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void ProcessMinorCharacter()
        {

        }
        private void MovingMinorCharacter()
        {
            bool kq = false;
            while (true && listCharacter.Count > 0)
            {
                for (int i = 0; i < listCharacter.Count; i++)
                {
                    MinorCharacter minor = listCharacter[i] as MinorCharacter;
                    if(MiniMap != null)
                        kq = minor.IdleAction(MiniMap);
                    else
                        kq = minor.IdleAction(WorldMap);
                    if (kq == true)
                    {
                        minor.movingStatus = 0;
                        minor.movingStatus = 1;
                        Thread.Sleep(75);
                        minor.movingStatus = 2;
                        Thread.Sleep(75);
                        minor.movingStatus = 0;
                    }
                    Thread.Sleep(2000);
                }
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Map map = new Map();
            if (MiniMap != null)
                map = MiniMap;
            else
                map = WorldMap;
            if (e.KeyCode == Keys.Left)
            {
                this.MoveGameCharacter(this.character1, MovingDirection.Left, map);
            }
            else if (e.KeyCode == Keys.Right)
            {
                this.MoveGameCharacter(this.character1, MovingDirection.Right, map);
            }
            else if (e.KeyCode == Keys.Up)
            {
                this.MoveGameCharacter(this.character1, MovingDirection.Up, map);
            }
            else if (e.KeyCode == Keys.Down)
            {
                this.MoveGameCharacter(this.character1, MovingDirection.Down, map);
            }

        }

        protected void LoadGameResource()
        {
            this.htMapPattern.Clear();

            string path = Path.Combine(GlobalVariable.appPath, "MapImages\\DiDuoc");
            string[] sar = Directory.GetFiles(path, "*.png");
            for (int i = 0; i < sar.Length; i++)
            {
                string fileName = sar[i];
                FileInfo fi = new FileInfo(fileName);

                string key = fi.Name;
                key = key.Substring(0, key.Length - 4);
                key = key.ToLower().Trim();

                Image im = Image.FromFile(fileName);
                this.htMapPattern.Add(int.Parse(key), im);
            }

            path = Path.Combine(GlobalVariable.appPath, "MapImages\\KhongDiDuoc");
            sar = Directory.GetFiles(path, "*.png");
            for (int i = 0; i < sar.Length; i++)
            {
                string fileName = sar[i];
                FileInfo fi = new FileInfo(fileName);

                string key = fi.Name;
                key = key.Substring(0, key.Length - 4);
                key = key.ToLower().Trim();

                Image im = Image.FromFile(fileName);
                this.htMapPattern.Add(int.Parse(key), im);
            }
        }

        protected int FixBoundaryX(int x)
        {
            if (x < 0)
                return 0;
            else if (x > this.mapW)
                return this.mapW;
            else
                return x;
        }

        protected int FixBoundaryY(int y)
        {
            if (y < 0)
                return 0;
            else if (y > this.mapH)
                return this.mapH;
            else
                return y;
        }

        protected int ConvertToScreenX(int x)
        {
            return (x * this.cellW);
        }

        protected int ConvertToScreenY(int y)
        {
            return (y * this.cellH);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            // Call the OnPaint method of the base class.
            base.OnPaint(e);

        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
            if(MiniMap != null)
                this.DrawScene(e.Graphics, MiniMap);
            else
                this.DrawScene(e.Graphics, WorldMap);
        }
        protected void DrawScene(Graphics g, Map map)
        {
            
            Bitmap bm = new Bitmap(this.cellW * this.viewW, this.cellH * this.viewH);
            Graphics g2 = Graphics.FromImage(bm);
            Brush brush = null;

            brush = new SolidBrush(Color.Black);
            g2.FillRectangle(brush, 0, 0, bm.Width, bm.Height);
            brush.Dispose();

            int startX = this.FixBoundaryX(this.viewX);
            int endX = this.FixBoundaryX(this.viewX + this.viewW);
            int startY = this.FixBoundaryY(this.viewY);
            int endY = this.FixBoundaryY(this.viewY + this.viewH);

            for (int y = startY; y < endY; y++)
            {
                for (int x = startX; x < endX; x++)
                {
                    DrawCell(g2, startX, startY, x, y, character1.z, map);
                }
            }

            this.DrawAllGameCharacters(g2);

            g2.Dispose();

            g.DrawImageUnscaled(bm, new Point());
            bm.Dispose();
           
        }

        protected void DrawCell(Graphics g, int startX, int startY, int x, int y, int z,Map map)
        {
            //MapItem item = this.map[x, y];
            int key = map.MAP[x, y, z];
            Image im = (Image)this.htMapPattern[key];

            int scrX = this.ConvertToScreenX(x - startX);
            int scrY = this.ConvertToScreenY(y - startY);

            g.DrawImageUnscaled(im, new Point(scrX, scrY));
        }

        protected void DrawGameCharacter(Graphics g, GameCharacter character)
        {
            int charViewX = character.x - this.viewX;
            int charViewY = character.y - this.viewY;

            if (charViewX < 0 || charViewX >= this.viewW ||
                charViewY < 0 || charViewY >= this.viewH)
            {
                return;
            }

            int scrX = this.ConvertToScreenX(charViewX);
            int scrY = this.ConvertToScreenX(charViewY) - 24;

            int idx = character.movingDirection * 3 + character.movingStatus;
            g.DrawImageUnscaled(character.lstImg[idx], new Point(scrX, scrY));
        }
        protected void DrawMinorCharacter(Graphics g, MinorCharacter minor)
        {
            
            int charViewX = minor.x - this.viewX;
            int charViewY = minor.y - this.viewY;

            if (charViewX < 0 || charViewX >= this.viewW ||
                charViewY < 0 || charViewY >= this.viewH)
            {
                return;
            }

            int scrX = this.ConvertToScreenX(charViewX);
            int scrY = this.ConvertToScreenX(charViewY) - 24;

            int idx = minor.movingDirection * 3 + minor.movingStatus;
            g.DrawImageUnscaled(minor.lstImg[idx], new Point(scrX, scrY));
        }
        protected void DrawAllGameCharacters(Graphics g)
        {
            this.DrawGameCharacter(g, this.character1);
            if (listCharacter != null)
            {
                for (int i = 0; i < listCharacter.Count; i++)
                {
                    MinorCharacter minor = listCharacter[i] as MinorCharacter;
                    this.DrawMinorCharacter(g, minor);
                }
            }
            
        }

        protected void Sleep(int time)
        {
            for (int i = 0; i < 4; i++)
            {
                Thread.Sleep(time);
                Application.DoEvents();
            }
        }

        protected void MoveGameCharacter(GameCharacter character, MovingDirection movDir, Map map)
        {
            //Graphics g = this.CreateGraphics();
            if (character.movingStatus != 0)
                return;

            int[] dx = new int[] { -1, 1, 0, 0 };
            int[] dy = new int[] { 0, 0, -1, 1 };

            int dirIdx = (int)movDir;
            int newX = character.x + dx[dirIdx];
            int newY = character.y + dy[dirIdx];
            if(listCharacter != null)
            {
                for (int i = 0; i < listCharacter.Count; i++)
                {
                    MinorCharacter minor = listCharacter[i] as MinorCharacter;
                    if (minor != null)
                    {
                        if (newX == minor.x)
                        {
                            if (newY == minor.y)
                            {
                                return;
                            }
                        }
                        if (newY == minor.y)
                        {
                            if (newX == minor.x)
                            {
                                return;
                            }
                        }
                    }
                }
            }
            
            
            if (newX < 0 || newX >= this.mapW ||
                newY < 0 || newY >= this.mapH)
            {
                return;
            }
            int curPos = map.MAP[newX, newY, character1.z];
            if (curPos > 999 && curPos < 1999)
            {
                int newMap = curPos - 1000;
                listCharacter = (ArrayList)htMinorCharacterArray[newMap];
                WorldMap = null;
                MiniMap = new Map();
                MiniMap.LoadMetaMap(newMap);
                MiniMap.LoadMap(newMap);
                character1.x = 11;
                character1.y = 11;
                character1.z = 0;
                
                //this.viewX = 3;
                //this.viewY = 0;
                //Invalidate();
                return;
            }
            if (curPos > 1999 && curPos < 2999)
            {
                int newMap = curPos - 2000 - 1;
                listCharacter = (ArrayList)htMinorCharacterArray[newMap];
                MiniMap = null;
                MiniMap = new Map();
                MiniMap.LoadMetaMap(newMap);
                MiniMap.LoadMap(newMap);
                character1.x = 11;
                character1.y = 11;
                character1.z = 0;

                //this.viewX = 3;
                //this.viewY = 0;
                //Invalidate();
                return;
            }
            if (map.MAP[newX, newY, character1.z] == 71)
            {
                MiniMap = null;
                WorldMap = new Map();
                WorldMap.LoadMetaMap(0);
                WorldMap.LoadMap(0);
                character1.x = 7;
                character1.y = 6;
                character1.z = 0;
                listCharacter = (ArrayList)htMinorCharacterArray[0];
                character.movingDirection = 3;
                this.viewX = 0;
                this.viewY = 0;
                //Invalidate();
                return;
            }
            if (curPos > 100000)
            {
                return;
            }

            int maxViewX = this.mapW - this.viewW;
            int newViewX = newX - (this.viewW >> 1);
            if (newViewX < 0)
                newViewX = 0;
            if (newViewX >= maxViewX)
                newViewX = maxViewX;

            int maxviewY = this.mapH - this.viewH;
            int newviewY = newY - (this.viewH >> 1);
            if (newviewY < 0)
                newviewY = 0;
            if (newviewY >= maxviewY)
                newviewY = maxviewY;

            character.movingStatus = 1;
            

            if (MovingDirection.Left == movDir)
            {
                character.movingDirection = 0;
            }
            else if (MovingDirection.Right == movDir)
            {
                character.movingDirection = 1;
            }
            else if (MovingDirection.Up == movDir)
            {
                character.movingDirection = 2;
            }
            else {
                character.movingDirection = 3;
            }

            Sleep(10);
            character.movingStatus = 2;
            Sleep(10);

            this.viewX = newViewX;
            this.viewY = newviewY;

            character.x = newX;
            character.y = newY;
            character.movingStatus = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        { 
            Invalidate();
        }
        public void MyMessageBox(string msg)
        {
            Graphics g = this.CreateGraphics();
            Rectangle rec = new Rectangle(50, 750, this.ClientSize.Width - 100, this.ClientSize.Height - 800);
            SolidBrush brushTwo = new SolidBrush(Color.Black);
            Image newImage = Image.FromFile(GlobalVariable.appPath + "\\Images\\msg.png");
            g.DrawImage(newImage, rec);
            
            Font font = new Font("Arial", 20);
            if (msg.Length > 100)
            {
                int i = 100;
                char c = msg[i];
                while (c.ToString() != " ")
                {
                    i--;
                    c = msg[i];
                    if (c.ToString() == " ")
                    {
                        break;  
                    }
                }
                string first = msg.Substring(0, i);
                string last = msg.Remove(0, i);
                g.DrawString(first, font, brushTwo, 100, 780.0f);
                g.DrawString(last, font, brushTwo, 100, 780.0f + 50);
            }
            else {
                g.DrawString(msg, font, brushTwo, 100, 780.0f);
            }
            g.Dispose();
        }
        int HP_Remain = 0;
        int myHP = 0;
        int yourHP = 0;
        int myHPFull = 0;
        int yourHPFull = 0;
        int i_Fighting = 0; // 
        int CanPressX = 0; // Trận đấu đang diễn ra thì ko đc bấm x
        int YourHP_remaining = 0;
        int MyHP_remaining = 0;
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
            if (e.KeyChar == 'z' || e.KeyChar == 'Z')
            {
                if (GlobalVariable.choice == 0)
                {
                    for (int i = 0; i < listCharacter.Count; i++)
                    {
                        MinorCharacter minor = listCharacter[i] as MinorCharacter;
                        if (character1.x == minor.x)
                        {
                            if (character1.y - 1 == minor.y || character1.y + 1 == minor.y)
                            {
                                CanPressX = 0;
                                timer1.Stop();
                                threadXuLyNhanVatPhuVaCayCanh.Suspend();
                                if (GlobalVariable.kichBan == 3 && minor.name == "Sam")
                                {
                                    DrawFighting();
                                    int j = 0;
                                    for (j = 0; j < 700; j += 50)
                                        DrawPokemonMoveAnimated(j);
                                    DrawFighting();
                                    DrawMessageFighting("msg2.png", "What should you do with your Pokemon?", 500);
                                    DrawMenuBottom(920, 800);
                                    DrawPokemonMoveAnimated(650);
                                    DrawChoice_1(920, 800);
                                    DrawHP1(120, 120);
                                    DrawHP2(750, 550);
                                    GlobalVariable.choice = 1;
                                    CanPressX = 1;
                                    return;
                                }
                                else {
                                    string msg = (string)minor.htMessage[GlobalVariable.kichBan];
                                    if (msg != null)
                                    {
                                        MyMessageBox(msg);
                                        if (GlobalVariable.kichBan == 1 && minor.name == "Tri")
                                        {
                                            GlobalVariable.kichBan++;
                                            return;
                                        }
                                        if (GlobalVariable.kichBan == 2 && minor.name == "Sam")
                                        {
                                            GlobalVariable.kichBan++;
                                            return;
                                        }
                                    }
                                    else
                                        timer1.Start();
                                }
                               
                            }
                        }
                        if (character1.y == minor.y)
                        {
                            if (character1.x - 1 == minor.x || character1.x + 1 == minor.x)
                            {
                                CanPressX = 0;
                                timer1.Stop();
                                threadXuLyNhanVatPhuVaCayCanh.Suspend();
                                if (GlobalVariable.kichBan == 3 && minor.name == "Sam")
                                {
                                    DrawFighting();
                                    int j = 0;
                                    for (j = 0; j < 700; j += 50)
                                        DrawPokemonMoveAnimated(j);
                                    DrawFighting();
                                    DrawMessageFighting("msg2.png", "What should you do with your Pokemon?", 500);
                                    DrawMenuBottom(920, 800);
                                    DrawPokemonMoveAnimated(650);
                                    DrawChoice_1(920, 800);
                                    DrawHP1(120, 120);
                                    DrawHP2(750, 550);
                                    GlobalVariable.choice = 1;
                                    CanPressX = 1;
                                    return;
                                }
                                else
                                {
                                    string msg = (string)minor.htMessage[GlobalVariable.kichBan];
                                    if (msg != null)
                                    {
                                        MyMessageBox(msg);
                                        if (GlobalVariable.kichBan == 1 && minor.name == "Tri")
                                        {
                                            GlobalVariable.kichBan++;
                                            return;
                                        }
                                        if (GlobalVariable.kichBan == 2 && minor.name == "Sam")
                                        {
                                            GlobalVariable.kichBan++;
                                            return;
                                        }
                                    }
                                    else
                                        timer1.Start();
                                }
                            }
                        }
                        
                    }
                }
                else if(GlobalVariable.choice == 1){
                    CanPressX = 0;
                    GlobalVariable.choice = 2;
                    GlobalVariable.choice_2 = 1;
                    DrawMenuMartialArts(60, 795);
                    DrawLoaiVoCong();
                    DrawChoice_1(60, 795);
                }
                else if (GlobalVariable.choice == 2)
                {
                    CanPressX = 1;
                    if (i_Fighting == -1)
                    {
                        timer1.Start();
                        GlobalVariable.choice = 0;
                        return;
                    }
                    
                    float hp = 0;
                    if(GlobalVariable.choice_2 == 1)
                    {
                        Pokemon myPKM = new Pokemon();
                        myPKM.Name = "Pikachu";
                        myPKM.getPropertiesOfCharacter("Pikachu");
                        Pokemon yourPKM = new Pokemon();
                        yourPKM.Name = "Astronault";
                        yourPKM.getPropertiesOfCharacter("Astronault");
                        int kq_chungcuoc = 0;
                        if (i_Fighting == 0)
                        {
                            myHPFull = myPKM.Hp;
                            yourHPFull = yourPKM.Hp;
                            MyHP_remaining = 270;
                            YourHP_remaining = 270;
                        }
                        else {
                            myPKM.Hp = myHP;
                            yourPKM.Hp = yourHP;
                        }
                        
                        i_Fighting = 1;
                       
                        yourHP = MyPokemonAttackYou(myPKM, yourPKM, yourHPFull);
                        DrawTuyetChieu();
                        YourHP_remaining = HP_Remain;
                        DrawHP3(340, 238, (int)YourHP_remaining);
                        DrawHP3(1000, 643, (int)MyHP_remaining);
                        if (yourHP > 0)
                        {
                            myHP = MyPokemonAttackYou(yourPKM, myPKM, myHPFull);
                            MyHP_remaining = HP_Remain;
                            DrawTuyetChieu();
                            DrawHP3(1003, 643, (int)MyHP_remaining);
                            DrawHP3(340, 238, (int)YourHP_remaining);
                            CanPressX = 0;
                            if (myHP <= 0)
                            {
                                kq_chungcuoc = 2;
                                i_Fighting = -1;
                                DrawMessageFighting("msg2.png", "Bạn thua rồi, Pokemon của bạn cần được hồi phục", 0);
                                GlobalVariable.kichBan--;
                                GlobalVariable.kichBan--;
                                CanPressX = 1;
                                return;
                            }
                        }
                        else
                        {
                            kq_chungcuoc = 1;
                            i_Fighting = -1;
                            DrawMessageFighting("msg2.png", "Bạn thắng cuộc, Pokemon của hắn là một con gà", 0);
                            GlobalVariable.kichBan++;
                            CanPressX = 1;
                            return;
                        }
                        DrawMenuMartialArts(60, 795);
                        DrawLoaiVoCong();
                    }
                    
                    //g.Dispose();
                }
            }
            else if(e.KeyChar == 'x' || e.KeyChar == 'X')
            {
                if (CanPressX == 0) // Trận đấu đang diễn ra không đc bấm X
                {
                    if (GlobalVariable.choice != 0)
                        GlobalVariable.choice--;
                    if (GlobalVariable.choice == 1)
                    {
                        DrawMenuBottom(920, 800);
                        DrawMessageFighting("msg2.png", "What should you do with your Pokemon?", 500);
                        CanPressX = 1;
                    }
                    else
                    {
                        timer1.Start();
                    }
                    threadXuLyNhanVatPhuVaCayCanh.Resume();
                    //////////////////////////////////////////////////////////////////////////////////////////////// ?????
                }
                else if (CanPressX == 1)
                {
                    // Không làm gì hết.
                }
            }
        }

        private void DrawTuyetChieu()
        {
            int x = 400;
            int y = 600;
            DrawMartialArts(x, y);
            for (int j = 0; j < 700; j += 250)
            {
                DrawMartialArts(x += 150, y -= 100);
                Sleep(20);
            }
            Graphics g = this.CreateGraphics();
            DrawFighting();
            DrawPokemon_2(650, g);
            DrawPokemon_1(650, g);
            DrawPokemon_1(650, g);
            DrawPokemon_1(650, g);
            DrawPokemon_1(650, g);

            DrawHP1(120, 120);
            DrawHP2(750, 550);
            g.Dispose();
        }
        private int MyPokemonAttackYou(Pokemon myPKM, Pokemon yourPKM, int HpFullOfPokemon)
        {
            int atk = myPKM.getMotLoaiVoCong("Tackle", myPKM);
            DrawMessageFighting("msg2.png", myPKM.Name + " sử dùng loại võ công Tackle", 0);
            Sleep(500);
            
            int atk_doithu = yourPKM.Attack;
            int def_doithu = yourPKM.Defense;
            int hp_doithu = yourPKM.Hp;
            int kq_tancong = hp_doithu - (atk - def_doithu);

            if (kq_tancong <= 0)
            {
                HP_Remain = 0;
                return 0;
            }
            HP_Remain = 270 * kq_tancong / HpFullOfPokemon;
            return kq_tancong;
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(1);
        }

        private void Form1_MaximumSizeChanged(object sender, EventArgs e)
        {
            cellW = 48;
            cellH = 48;
            viewW = 29;
            viewH = 20;
            Invalidate();
        }
        private void DrawMessageFighting(string imgPath, string text, int width)
        {
            Graphics g = this.CreateGraphics();
            ///////////////////////////////////////////////////////////////////////////////////////////////////////// 
            Rectangle rec1 = new Rectangle(0, 760, this.ClientSize.Width - width, 200);
            Image newImage1 = Image.FromFile(GlobalVariable.appPath + "\\Images\\" + imgPath);
            g.DrawImage(newImage1, rec1);
            ///////////////////////////////////////////////////////////////////////////////////////////////////////// 
            
            /////////////////////////////////////////////////////////////////////////////////////////////////////////
            Font font = new Font("Arial", 30);
            SolidBrush brushTwo = new SolidBrush(Color.White);
            g.DrawString(text, font, brushTwo, 70, 800.0f);
            /////////////////////////////////////////////////////////////////////////////////////////////////////////
            g.Dispose();
        }
        private void DrawFighting()
        {
            /////////////////////////////VẼ SÂN ĐẤU/////////////////////////////////////// 
            Graphics g = this.CreateGraphics();
            Rectangle rec = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height-200);
            Image newImage = Image.FromFile(GlobalVariable.appPath + "\\Images\\san1.png");
            g.DrawImage(newImage, rec);
            GlobalVariable.choice_1 = 1;
            //Sleep(10);
            g.Dispose();
        }

        private void DrawMenuBottom(int x, int y)
        {
            // Vẽ menu : FIGHT - BAG - POKEMON - RUN
            Graphics g = this.CreateGraphics();
            Rectangle rec2 = new Rectangle(880, 760, 515, 200);
            Image newImage2 = Image.FromFile(GlobalVariable.appPath + "\\Images\\menu-1.png");
            g.DrawImage(newImage2, rec2);
            DrawChoice_1(x,y);
            g.Dispose();
        }
        private void DrawMenuMartialArts(int x, int y)
        {
            // Vẽ menu chọn loại tuyệt chiêu
            Graphics g = this.CreateGraphics();
            Rectangle rec2 = new Rectangle(0, 760, this.ClientSize.Width, this.ClientSize.Height - 770);
            Image newImage2 = Image.FromFile(GlobalVariable.appPath + "\\Images\\menu-2.png");
            g.DrawImage(newImage2, rec2);
            DrawChoice_1(x, y);
            g.Dispose();
            GlobalVariable.choice = 2;
        }
        private void DrawLoaiVoCong()
        {
            // Vẽ menu chọn loại tuyệt chiêu
            Graphics g = this.CreateGraphics();
            Pokemon pkm = new Pokemon();
            Hashtable htLoaiVoCong = new Hashtable();
            htLoaiVoCong = pkm.LoadLoaiVoCong("Pikachu");
            Font font = new Font("Arial", 30);
            SolidBrush brushTwo = new SolidBrush(Color.Black);
            int x = 70;
            int y = 800;
            foreach (DictionaryEntry entry in htLoaiVoCong)
	        {
                
	            g.DrawString(entry.Key.ToString(), font, brushTwo, x, y);
                if (x == 470)
                {
                    if (y == 865)
                    {
                        x = x + 400;
                    }
                    else 
                    {
                        y = y + 65;
                        x = 70;
                    }
                }
                else
                {
                    x = x + 400;
                }   
	        }
            
            g.Dispose();
            GlobalVariable.choice = 2;
        }
        private void DrawMartialArts(int x, int y)
        {
            // Vẽ tuyệt chiêu
            Graphics g = this.CreateGraphics();
            Rectangle rec2 = new Rectangle(x, y, 150, 150);
            Image newImage2 = Image.FromFile(GlobalVariable.appPath + "\\MartialArts\\1.png");
            g.DrawImage(newImage2, rec2);
            //DrawChoice_1(x, y);
            g.Dispose();
            GlobalVariable.choice = 2;
        }
        private void DrawHP1(int x, int y)
        {
            // Vẽ máu của nhân vật
            Graphics g = this.CreateGraphics();
            Rectangle rec2 = new Rectangle(x, y, 550, 200);
            Image newImage2 = Image.FromFile(GlobalVariable.appPath + "\\Images\\hp-1.png");
            g.DrawImage(newImage2, rec2);
            //DrawChoice_1(x, y);
            g.Dispose();
            GlobalVariable.choice = 2;
        }
        private void DrawHP2(int x, int y)
        {
            // Vẽ máu của nhân vật
            Graphics g = this.CreateGraphics();
            Rectangle rec2 = new Rectangle(x, y, 550, 200);
            Image newImage2 = Image.FromFile(GlobalVariable.appPath + "\\Images\\hp-2.png");
            g.DrawImage(newImage2, rec2);
            //DrawChoice_1(x, y);
            g.Dispose();
            GlobalVariable.choice = 2;
        }
        private void DrawHP3(int x, int y, int width)
        {
            // Vẽ ống máu của nhân vật 1
            Graphics g = this.CreateGraphics();
            Rectangle rec1 = new Rectangle(x, y, 270, 20);
            Rectangle rec2 = new Rectangle(x, y, width, 20);
            Image newImage1 = Image.FromFile(GlobalVariable.appPath + "\\Images\\hp-3.png");
            Image newImage2 = Image.FromFile(GlobalVariable.appPath + "\\Images\\hp-4.png");
            g.DrawImage(newImage1, rec1);
            g.DrawImage(newImage2, rec2);
            g.Dispose();
            GlobalVariable.choice = 2;
        }
        private void DrawChoice_1(int x, int y)
        {
            Graphics g = this.CreateGraphics();
            Rectangle rec = new Rectangle(920, 800, 220, 55);
            if(GlobalVariable.choice_1 == 2) // Xuống
                //rec = new Rectangle(920, 870, 220, 55);
                rec = new Rectangle(x, y, 220, 55);
            if (GlobalVariable.choice_1 == 3) // Qua phải
                //rec = new Rectangle(1140, 800, 220, 55);
                rec = new Rectangle(x, y, 220, 55);
            if (GlobalVariable.choice_1 == 4) // Lên
                //rec = new Rectangle(1140, 800, 220, 55);
                rec = new Rectangle(x, y, 220, 55);
            if (GlobalVariable.choice_1 == 1) // Qua trái
                //rec = new Rectangle(920, 800, 220, 55);
                rec = new Rectangle(x, y, 220, 55);
            SolidBrush newBrush = new SolidBrush(Color.Red);
            Pen pen = new Pen(newBrush);
            pen.Width = 5;
            g.DrawRectangle(pen, rec);
            g.Dispose();
        }
        private void DrawPokemonMoveAnimated(int i)
        {
            Graphics g = this.CreateGraphics();
            DrawPokemon_1(i, g);
            DrawPokemon_2(i, g);
            g.Dispose();
        }

        private static void DrawPokemon_1(int i, Graphics g)
        {
            Rectangle rec1 = new Rectangle();

            rec1.X = i + 250;
            rec1.Y = 250;
            rec1.Width = i - 400;
            rec1.Height = 200;

            Image newImage1 = Image.FromFile(GlobalVariable.appPath + "\\PokemonStatic\\2.png");
            SolidBrush brushTwo = new SolidBrush(Color.Black);
            Pen p = new Pen(brushTwo);
            //g.DrawRectangle(p, rec1);
            g.DrawImage(newImage1, rec1);
        }

        private void DrawPokemon_2(int i, Graphics g)
        {
            Rectangle rec2 = new Rectangle();
            rec2.X = ClientSize.Width - i * 2 + 100;
            rec2.Y = 500;
            rec2.Width = 300;
            rec2.Height = 250;

            Image newImage2 = Image.FromFile(GlobalVariable.appPath + "\\PokemonStatic\\3.png");
            //g.DrawRectangle(p, rec2);
            g.DrawImage(newImage2, rec2);
 
        }
        private void Fighting()
        {

        }
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (GlobalVariable.choice == 1) // Lựa chọn đầu tiên : Fight - bag - run ...
            {
                if (e.KeyCode == Keys.Down)
                {
                    if (GlobalVariable.choice_1 == 1)
                    {
                        DrawMenuBottom(920, 870);
                        GlobalVariable.choice_1 = 2;
                    } else if (GlobalVariable.choice_1 == 4)
                    {
                        DrawMenuBottom(1140, 870);
                        GlobalVariable.choice_1 = 3;
                    }
                    
                }
                else if (e.KeyCode == Keys.Right)
                { 
                    if (GlobalVariable.choice_1 == 1)
                    {
                        DrawMenuBottom(1140, 800);
                        GlobalVariable.choice_1 = 4;
                    }
                    else if (GlobalVariable.choice_1 == 2)
                    {
                        DrawMenuBottom(1140, 870);
                        GlobalVariable.choice_1 = 3;
                    }
                }
                else if (e.KeyCode == Keys.Up)
                { 
                    if (GlobalVariable.choice_1 == 2)
                    {
                        DrawMenuBottom(920, 800);
                        GlobalVariable.choice_1 = 1;
                    }
                    else if (GlobalVariable.choice_1 == 3)
                    {
                        DrawMenuBottom(1140, 800);
                        GlobalVariable.choice_1 = 4;
                    }
                }
                else if (e.KeyCode == Keys.Left)
                {
                    if (GlobalVariable.choice_1 == 4)
                    {
                        DrawMenuBottom(920, 800);
                        GlobalVariable.choice_1 = 1;
                    }
                    else if (GlobalVariable.choice_1 == 3)
                    {
                        DrawMenuBottom(920, 870);
                        GlobalVariable.choice_1 = 2;
                    }
                    
                }
            }
            else if (GlobalVariable.choice == 2) // Lựa chọn tuyệt chiêu
            {
                if (e.KeyCode == Keys.Down)
                {
                    if (GlobalVariable.choice_2 == 1)
                    {
                        DrawMenuMartialArts(60, 860);
                        GlobalVariable.choice_2 = 2;
                    }
                    else if (GlobalVariable.choice_2 == 4)
                    {
                        DrawMenuMartialArts(460, 860);
                        GlobalVariable.choice_2 = 3;
                    }
                    DrawLoaiVoCong();
                }
                else if (e.KeyCode == Keys.Right)
                {
                    if (GlobalVariable.choice_2 == 1)
                    {
                        DrawMenuMartialArts(460, 795);
                        GlobalVariable.choice_2 = 4;
                    }
                    else if (GlobalVariable.choice_2 == 2)
                    {
                        DrawMenuMartialArts(460, 860);
                        GlobalVariable.choice_2 = 3;
                    }
                    DrawLoaiVoCong();
                }
                else if (e.KeyCode == Keys.Up)
                {
                    if (GlobalVariable.choice_2 == 2)
                    {
                        DrawMenuMartialArts(60, 795);
                        GlobalVariable.choice_2 = 1;
                    }
                    else if (GlobalVariable.choice_2 == 3)
                    {
                        DrawMenuMartialArts(460, 795);
                        GlobalVariable.choice_2 = 4;
                    }
                    DrawLoaiVoCong();
                }
                else if (e.KeyCode == Keys.Left)
                {
                    if (GlobalVariable.choice_2 == 4)
                    {
                        DrawMenuMartialArts(60, 795);
                        GlobalVariable.choice_2 = 1;
                    }
                    else if (GlobalVariable.choice_2 == 3)
                    {
                        DrawMenuMartialArts(60, 860);
                        GlobalVariable.choice_2 = 2;
                    }
                    DrawLoaiVoCong();
                }
                
            }
        }
    }
}
//Trí ™ (1/18/2012 10:06:40 PM): như vậy tui xây dựng 1 cái class gọi là Pokemos
//Trí ™ (1/18/2012 10:06:42 PM): như vậy tui xây dựng 1 cái class gọi là Pokemon
//Trí ™ (1/18/2012 10:06:58 PM): trong đó có các thuộc tính cơ bản như attack, def....
//Trí ™ (1/18/2012 10:07:10 PM): nhân vật chính của tui sẽ có 1 cái mảng các con này
//Meo Con (1/18/2012 10:07:20 PM): uh
//Trí ™ (1/18/2012 10:07:57 PM): rồi mấy cái atk và def của từng con mình lưu ra file hay là trong code?
//Meo Con (1/18/2012 10:08:15 PM): file chu
//Trí ™ (1/18/2012 10:08:52 PM): ví dụ con đó atk 50 def 25
//Trí ™ (1/18/2012 10:08:54 PM): lv1
//Trí ™ (1/18/2012 10:09:05 PM): lv5 thì các chỉ số đó tăng như thế nào?
//Meo Con (1/18/2012 10:09:26 PM): cai nay tuy ong wy uoc thoi, ko nhat thiet cu fai lam giong het game
//Meo Con (1/18/2012 10:09:45 PM): trong game thi cu len lvl thi cac chi so se dc + 1
//Meo Con (1/18/2012 10:10:00 PM): moi con co 1 chi so co ban, goi la base
//Meo Con (1/18/2012 10:10:13 PM): tuong ung se co base atk, base def, ....
//Meo Con (1/18/2012 10:10:25 PM): day la chi so luc moi bat dau lvl1 cua tung con
//Meo Con (1/18/2012 10:10:49 PM): cai nay ghi trong file ( trong game thi luu trong file gba )
//Meo Con (1/18/2012 10:11:06 PM): khi len lvl, no co cong thuc tinh
//Meo Con (1/18/2012 10:11:16 PM): nhung ma ko can fai tinh giong no dau
//Meo Con (1/18/2012 10:11:29 PM): cu lam don gian, len 1 lvl thi tang moi thu len 1 la dc roi
//Trí ™ (1/18/2012 10:12:01 PM): tăng mình lưu như thế nào
//Trí ™ (1/18/2012 10:12:09 PM): cái base trong file là ko thay đổi đúng ko?
//Trí ™ (1/18/2012 10:12:45 PM): mình tự tăng trong code lv1 thì +1
//Trí ™ (1/18/2012 10:12:54 PM): chứ ko cần lưu luôn hay cần lưu
//Meo Con (1/18/2012 10:13:01 PM): uh, base ko doi
//Meo Con (1/18/2012 10:13:19 PM): thuc ra trong game no lam fuc tap hon
//Meo Con (1/18/2012 10:13:32 PM): moi con co 1 cach tang rieng
//Meo Con (1/18/2012 10:13:51 PM): co con tang atk rat nhanh, nhung def thi lau lau moi tang
//Meo Con (1/18/2012 10:14:03 PM): co con thi def tang nhanh ma speed tang cham, ...
//Meo Con (1/18/2012 10:14:16 PM): neu ong don gian thi cu tang het theo lvl
//Meo Con (1/18/2012 10:14:46 PM): con neu muon lam ky nhu no thi o fai luu them trong file inc ratio nua
//Meo Con (1/18/2012 10:14:54 PM): vi du co nao thien ve suc manh co bap
//Meo Con (1/18/2012 10:15:06 PM): thi increase ratio atk cua no la 1
//Meo Con (1/18/2012 10:15:40 PM): de bu tru lai thi ratio def cua no la 0.2, tuc la len 5 level no moi tang dc 1 def
//    Meo Con (1/18/2012 10:16:38 PM): uh
//Meo Con (1/18/2012 10:16:49 PM): neu danh manh ma ben kia co the chet ngay thi se thang 
//Meo Con (1/18/2012 10:17:01 PM): nhung neu ben kia chua chet ma no dap lai thi nguy
//Meo Con (1/18/2012 10:17:19 PM): no con fu thuoc thuoc tinh
//Meo Con (1/18/2012 10:17:38 PM): vi du lua danh nuoc se bi giam tu 30-50% suc tan cong
//Meo Con (1/18/2012 10:17:55 PM): ngc lai nc danh lua thi se tang tu 30-50% suc tan cong